from google.appengine.api import memcache
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
import sync, logging


#from ft_table import ft
import logging, model, os
from gae_db_manager import GaeDbManager
import datetime
from google.appengine.api import memcache



def remove_duplicate(tbl):
    '''
    remove records that were imported more than once
    '''
    logging.info('Checking for duplicates for %s' % tbl)
    gdm = GaeDbManager(tbl)
    count_list = {}
    for r in gdm.table.all():
        count_list.setdefault(r.ID, 0)
        count_list[r.ID]+=1
    
    
    for (idv, ct) in [x for x in count_list.items() if x[1]>1]:
        remove_list = []
        keep = None
        logging.info('Removing Duplicate for record ID = %s. (%s records found)' % (idv, ct))
        for r in gdm.table.all().filter('ID =', idv):
            if r.report_test_no:
                keep = r
            else:
                remove_list.append(r)
        if keep:
            #db.delete(remove_list)
            pass
        else:
            #if none of the test has a report_test_no, just pick up one
            keep = remove_list.pop()
        
        db.delete(remove_list)
        
        for msg in ['Remove : %s-%s' % (x.test_no, x.report_test_no) for x in remove_list]:
            logging.info(msg)
        logging.info('Keep: %s-%s' % (keep.test_no, keep.report_test_no))
                

            
        
                
            
        
           

class ManageHandler(webapp.RequestHandler):
    def get(self, action, tbl):
#        action = self.request.get('action')
        if action=='flush-memcache':
            memcache.flush_all()
            logging.info('flush memcache')
            self.response.out.write('memache is flushed.')
        elif action=='sync_all':
            sync.sync_all()
            self.response.out.write('All synced.')
        elif action=='update_table_schema':
#            tbl = self.request.get('tbl')
            gdm = GaeDbManager(tbl)
            gdm.update_schema()
            self.response.out.write('%s schema is updated.' % tbl)
        elif action=='empty_table':
            gdm = GaeDbManager(tbl)
            gdm.empty()
            self.response.out.write('%s is emptied.' % tbl)
        elif action=='download':
            path = os.path.join(os.path.dirname(__file__), 'template/download.html')
            options = 'asphalt_test compaction_test table_schema Email dlz_project_list dlz_connection'.split()
            self.response.out.write(template.render(path, {'options': options}))
        elif action=='removeduplicate':
            remove_duplicate(tbl)
        else:
            path = os.path.join(os.path.dirname(__file__), 'template/index.html')
            self.response.out.write(template.render(path, {'flush_memcache': 'flush-memcache', 'exe_failed_sql': 'exe_failed_sql', 'sync_all': 'sync_all'}))
    
        
    def exe_failed_sql(self):
        '''
        scan the SqlReqeust table and execute the failed sql
        '''
        f = ft(None, None)
        results = []
        for s in model.SqlRequest.all().filter('status =', 'new'):
            
            t = s.transaction_ref
            if t:
                desc = t.description
            else:
                desc = ''
            result = {'description': desc, 'sql': s.sql, 'status': 'Failed again'}
            try:
                f.SqlQuery(s.sql, t)
                result['status'] = 'success'
            except:
                pass
            s.delete()
            
            results.append(result)
        
        
        
        path = os.path.join(os.path.dirname(__file__), 'template/sql_list.html')
        self.response.out.write(template.render(path, {'results': results}))
            
            
        
            